Thread: segmentation fault [cant find whats the problem]

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    8

    segmentation fault [cant find whats the problem]

    Hi people,
    actually im working on this code, i cant figure out why im getting that error when trying to feel a matrix with a function(just to have all the things separated), can any one explain me? i working to become better in C.

    thank you in advice!!
    BLESS, Ovnyx

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    
    
    #define SHMEM_SIZE 1024
    
    
    typedef struct{
          char *name;
          char board[6][6]; // * -> agua, M -> barco
          int state;  //1 con barco, 0 sin barco
          int destroyed;    //cant de barcos destruidos
    }player;
    
    
    void fill_board(player *Player){
          for (size_t i = 0; i < 6; i++) {
                for (size_t j = 0; i < 6; j++) {
                      Player->board[i][j] = '*';
                }
          }
    }
    
    
    
    
    void create_player(player *Player){
          char x[20];
          printf("Ingrese su nombre:\n");
          scanf("%s", &x);
    
    
          Player->name = x;
          Player-> state = 0;
          Player-> destroyed = 0;
    
    
          fill_board(&(*Player));
    /*
          for (size_t i = 0; i < 6; i++) {
                for (size_t j = 0; j < 6; j++) {
                      Player->board[i][j] = '*';
                }
          }
          */
    }
    
    
    int main(int argc, char *argv[]) {
          player players[3];
          create_player(&players[0]);
          printf("Nombre: %s | Estado: %d | Barcos Destruidos: %d |\n", players[0].name, players[0].state, players[0].destroyed);
          printf("\n");
          for (size_t i = 0; i < 6; i++) {
               for (size_t j = 0; j < 6; j++) {
                    printf("|%c",players[0].board[i][j]);
                    if (j == 5) {
                         printf("|%c|\n",players[0].board[i][j]);
                    }
               }
          }
          printf("\n");
    
    
          return 0;
    }

  2. #2
    Guest
    Guest
    Code:
    void fill_board(player *Player){
          for (size_t i = 0; i < 6; i++) {
                for (size_t j = 0; i < 6; j++) { // Should be j < 6
                      Player->board[i][j] = '*';
                }
          }
    }
    Code:
    scanf("%s", &x);
    This gave me a compiler warning, I think you just want to pass the array, i.e.
    Code:
    scanf("%s", x);
    Code:
    fill_board(&(*Player));
    Recall that & and * work in opposite directions, so this should be the same as:
    Code:
    fill_board(Player);

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    wow thank you that was the error, so stupid i apologize! and thanks for the info really appreciate it!

    Bless.
    Last edited by ovnyx; 11-11-2017 at 09:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault and can`t find where.
    By heatblazer in forum C Programming
    Replies: 6
    Last Post: 09-22-2014, 05:51 AM
  2. Segmentation Fault, could not find after debugging
    By Campocalypse in forum C Programming
    Replies: 9
    Last Post: 04-15-2013, 11:53 PM
  3. Segmentation fault, cannot find problem
    By navitude in forum C Programming
    Replies: 4
    Last Post: 03-02-2013, 01:57 PM
  4. Segmentation fault - can't find it
    By überfuzz in forum C++ Programming
    Replies: 14
    Last Post: 11-29-2012, 11:40 PM
  5. Can't find the cause of a segmentation fault
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2006, 08:28 PM

Tags for this Thread